package com.kobe.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.kobe.domain.SysAssetCategory;
import com.kobe.mapper.CategoryMapper;
import com.kobe.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    @Override
    public List<Tree<String>> tree() {
        // 查询全部数据
        List<SysAssetCategory> nodeList = categoryMapper.selectAll();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("orderNum");
        treeNodeConfig.setIdKey("id");

        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
            (treeNode, tree) -> {
                tree.setId(treeNode.getId().toString());
                tree.setParentId(treeNode.getParentId().toString());
                tree.setWeight(treeNode.getOrderNum());
                // 扩展属性 ...
                tree.putExtra("categoryName", treeNode.getCategoryName());
                tree.putExtra("status", treeNode.getStatus());
                tree.putExtra("remark", treeNode.getRemark());
                tree.putExtra("createTime", treeNode.getCreateTime());
                tree.putExtra("creator", treeNode.getCreator());
           });
        return treeNodes;
    }
}
